﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_ServiceTypeManager : System.Web.UI.Page
{
    ServiceBlo _serviceBlo = new ServiceBlo();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            lblStatus.Visible = false;
            LoadGdvServiceType();
        }
    }
    private void LoadGdvServiceType()
    {
        gdvServiceType.DataSource = _serviceBlo.SelectAllServicesType();
        gdvServiceType.DataBind();
    }
    protected void gdvServiceType_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id_ST = int.Parse(gdvServiceType.DataKeys[e.RowIndex].Value.ToString());
        if (((LinkButton)gdvServiceType.Rows[e.RowIndex].Cells[2].FindControl("lbtnDeactive")).Visible == true)
        {
            _serviceBlo.UpdateServiceTypeStatus(id_ST, false);
            LoadGdvServiceType();
        }
        else
        {
            _serviceBlo.UpdateServiceTypeStatus(id_ST, true);
            LoadGdvServiceType();
        }
    }
    protected void gdvServiceType_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gdvServiceType.EditIndex = e.NewEditIndex;
        LoadGdvServiceType();        
    }
    protected void gdvServiceType_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gdvServiceType.EditIndex = -1;
        LoadGdvServiceType();
    }
    protected void gdvServiceType_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        try
        {           
            int id_ST = int.Parse(gdvServiceType.DataKeys[e.RowIndex].Value.ToString());
            string _serviceName = ((TextBox)gdvServiceType.Rows[e.RowIndex].Cells[0].Controls[0]).Text;
            ServiceDto _serviceDto = new ServiceDto(id_ST, _serviceName);
            _serviceBlo.UpdateServiceType(_serviceDto);
        }
        catch (Exception)
        {
            lblStatus.Visible = true;
            lblStatus.Text = " Your data is not valid! Please try again";
            gdvServiceType.EditIndex = e.RowIndex;
            LoadGdvServiceType();
            return;
        }
        gdvServiceType.EditIndex = -1;
        LoadGdvServiceType();
        lblStatus.Visible = true;
        lblStatus.Text = "Update successfull ";
    }
}